package com.zztest.aitestmini.controller;

import com.alibaba.fastjson.JSONObject;
import com.zztest.aitestmini.common.TokenDb;
import com.zztest.aitestmini.common.UserBaseStr;
import com.zztest.aitestmini.common.ZzToken;
import com.zztest.aitestmini.dto.BuildDto;
import com.zztest.aitestmini.dto.ResultDto;
import com.zztest.aitestmini.dto.TokenDto;
import com.zztest.aitestmini.dto.user.AddUserDto;
import com.zztest.aitestmini.dto.user.UpdateUserDto;
import com.zztest.aitestmini.dto.user.UserDto;
import com.zztest.aitestmini.entity.ZzTestUser;
import com.zztest.aitestmini.service.TestUserService;
import com.zztest.aitestmini.util.JenkinsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @Author: zhangcheng
 * @Description:
 * @Date: 2021/3/27/027 17:52
 * @Version: 1.0
 */
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("ZzUser")
@Slf4j
public class ZzTestController {
    @Autowired
    private TestUserService testUserService;
    @Autowired
    private TokenDb tokenDb;

    @ApiOperation("调用Jenkins构建job")
    @PutMapping("build")
    public ResultDto build(@RequestBody BuildDto buildDto) throws IOException, URISyntaxException {

        log.info("调用Jenkins构建job 请求入参 "+JSONObject.toJSONString(buildDto));
        JenkinsUtil.build(buildDto.getJobName(),buildDto.getUserId(),buildDto.getRemark(),buildDto.getTestCommand());
        return ResultDto.success("成功");
    }

    //用户注册
    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ResultDto<ZzTestUser> save(@RequestBody AddUserDto addUserDto){
        ZzTestUser zztestUser = new ZzTestUser();
        //使用spring 工具直接赋值
        BeanUtils.copyProperties(addUserDto,zztestUser);
        // 单独赋值 zztestUser.setEmail(addUserDto.getEmail());
        if(StringUtils.isEmpty(addUserDto.getPassword())){
            return ResultDto.fail("密码不能为空");
        }
        if (StringUtils.isEmpty(addUserDto.getUserName())){
            return ResultDto.fail("用户名不能为空");
        }
        //实体类转换为json
        log.info("请求入参"+JSONObject.toJSONString(zztestUser));
        return testUserService.save(zztestUser);
    }

    @ApiOperation("登录接口")
    // @RequestMapping(value = "login",method = RequestMethod.POST)
    @PostMapping(value = "login")  // 只能用于方法级别
    //将request body中的json/xm1对象解析成该参数类型的Javabean对象
    //   public  String login(@RequestBody UserDto userDto){
    public ResultDto<ZzToken> login(@RequestBody UserDto userDto){

        return  testUserService.login(userDto);

    }

    //用户更新
    @ApiOperation(value = "用户修改")
    @PutMapping
    public ResultDto<ZzTestUser> updateUserInfo(@RequestBody UpdateUserDto updateUserDto){
        ZzTestUser zztestUser = new ZzTestUser();
        //使用spring 工具直接赋值
        BeanUtils.copyProperties(updateUserDto,zztestUser);
        // 单独赋值 zztestUser.setEmail(addUserDto.getEmail());
        if(StringUtils.isEmpty(updateUserDto.getPassword())){
            return ResultDto.fail("密码不能为空");
        }
        if (StringUtils.isEmpty(updateUserDto.getUserName())){
            return ResultDto.fail("用户名不能为空");
        }
        //实体类转换为json
        log.info("请求入参"+JSONObject.toJSONString(zztestUser));

        return  testUserService.update(zztestUser);
    }

    @ApiOperation("查询是否已经登录")
    @GetMapping("isLogin")
    public ResultDto isLogin(HttpServletRequest request){

        //1、从请求的Header获取客户端附加token
        String tokenStr = request.getHeader(UserBaseStr.LOGIN_TOKEN);
        TokenDto tokenDto = tokenDb.getUserInfo(tokenStr);
        //更新token中信息
        /*tokenDto.setDefaultJenkinsId(10);
        tokenDb.addUserInfo(tokenDto.getToken(),tokenDto);*/
        return ResultDto.success("成功",tokenDto);
    }


    @ApiOperation("根据用户名模糊查询")
    @GetMapping("byName")
    public ResultDto<List<ZzTestUser>> getByName(@RequestParam (value = "userId",required = false) Integer userId,String userName){
        ZzTestUser zztestUser = new ZzTestUser();
        zztestUser.setId(userId);
        zztestUser.setUserName(userName);
        //实体类转换为json
        log.info("请求入参"+JSONObject.toJSONString(zztestUser));
        return  testUserService.getByName(zztestUser);

    }

    @ApiOperation("根据id删除")
    @DeleteMapping("{userId}")
    public ResultDto delete(@PathVariable (value = "userId") Integer userId){
        //实体类转换为json
        log.info("根据id删除用户"+userId);
        return testUserService.delete(userId);

    }


}
